﻿/* 2012/9/21 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Cosmos.Drawing
{
    /// <summary>
    /// Provides drawing information by specifying drawing style.
    /// </summary>
    public static class DrawingInfoProvider
    {
        public static DrawingInfo Center(Size imageSize, Size canvasSize)
        {
            double widthScale = (double)canvasSize.Width / (double)(imageSize.Width);
            double heightScale = (double)canvasSize.Height / (double)(imageSize.Height);

            double displayScale = Math.Min(widthScale, heightScale);

            if (displayScale > 1.0)
                displayScale = 1.0;

            int displayWidth = (int)((double)imageSize.Width * displayScale);
            int displayHeight = (int)((double)imageSize.Height * displayScale);

            DrawingInfo result = new DrawingInfo()
            {
                MinStartX = 0,
                MaxStartX = canvasSize.Width - displayWidth,
                MinStartY = 0,
                MaxStartY = canvasSize.Height - displayHeight,
                DisplayWidth = displayWidth,
                DisplayHeight = displayHeight,
                DisplayScale = displayScale,
            };
            return result;
        }

        public static DrawingInfo Fit(Size imageSize, Size canvasSize)
        {
            double scaleWidth = (double)canvasSize.Width / (double)(imageSize.Width);
            double scaleHeight = (double)canvasSize.Height / (double)(imageSize.Height);

            double displayScale = Math.Min(scaleWidth, scaleHeight);

            int displayWidth = (int)((double)imageSize.Width * displayScale);
            int displayHeight = (int)((double)imageSize.Height * displayScale);

            DrawingInfo result = new DrawingInfo()
            {
                MinStartX = 0,
                MaxStartX = canvasSize.Width - displayWidth,
                MinStartY = 0,
                MaxStartY = canvasSize.Height - displayHeight,
                DisplayWidth = displayWidth,
                DisplayHeight = displayHeight,
                DisplayScale = displayScale,
            };
            return result;
        }

        public static DrawingInfo Fill(Size imageSize, Size canvasSize)
        {
            double scaleWidth = (double)canvasSize.Width / (double)(imageSize.Width);
            double scaleHeight = (double)canvasSize.Height / (double)(imageSize.Height);

            double displayScale = Math.Max(scaleWidth, scaleHeight);

            int displayWidth = (int)((double)imageSize.Width * displayScale);
            int displayHeight = (int)((double)imageSize.Height * displayScale);

            DrawingInfo result = new DrawingInfo()
            {
                MinStartX = canvasSize.Width - displayWidth,
                MaxStartX = 0,
                MinStartY = canvasSize.Height - displayHeight,
                MaxStartY = 0,
                DisplayWidth = displayWidth,
                DisplayHeight = displayHeight,
                DisplayScale = displayScale,
            };
            return result;
        }

        public static DrawingInfo Zoom(Size imageSize, Size canvasSize, double displayScale)
        {
            int displayWidth = (int)((double)imageSize.Width * displayScale);
            int displayHeight = (int)((double)imageSize.Height * displayScale);

            DrawingInfo result = new DrawingInfo()
            {
                MinStartX = Math.Min(0, canvasSize.Width - displayWidth),
                MaxStartX = Math.Max(0, canvasSize.Width - displayWidth),
                MinStartY = Math.Min(0, canvasSize.Height - displayHeight),
                MaxStartY = Math.Max(0, canvasSize.Height - displayHeight),
                DisplayWidth = displayWidth,
                DisplayHeight = displayHeight,
                DisplayScale = displayScale,
            };
            return result;
        }
    }
}
